// 分类模块
import { topCategory } from '@/api/constants'
import { findAllCategory } from '@/api/category'
const state = () => {
  return {
    // 分类信息  本地获取数据，保证数据初始化不白屏
    list: topCategory.map(item => ({ name: item }))
  }
}

const mutations = {
  setList (state, payload) {
    state.list = payload
  },
  // 修改当前一级分类下的open数据为true
  show (state, item) {
    const category = state.list.find(category => category.id === item.id)
    category.open = true
  },
  // 修改当前一级分类下的open数据为false
  hide (state, item) {
    const category = state.list.find(category => category.id === item.id)
    category.open = false
  }
}

const actions = {
  async getList ({ commit }) {
    const { result } = await findAllCategory()
    // 控制二级分类显示隐藏
    result.forEach(item => {
      item.open = false
    })
    commit('setList', result)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
